package com.wyy.dao.impl;

import com.wyy.bean.Menu;
import com.wyy.bean.Role;
import com.wyy.dao.DBUtils;
import com.wyy.dao.RoleDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName RoleDaoImpl
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/27 8:20
 * @Version 1.0
 **/
public class RoleDaoImpl extends DBUtils implements RoleDao {
    @Override
    public List<Role> getList() {
        
        List<Role> roles = new ArrayList<>();
    
        try {
            String sql = "select * from role";
            resultSet = query(sql, null);
            while(resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
//                System.out.println(resultSet.getString("rolename"));
                roles.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
    
        return roles;
    }
    
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List<Role> roleList=new ArrayList<Role>();
        try {
            String sql="select * from role limit ?,?";
            List params=new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            
            resultSet = query(sql, params);
            while(resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                roleList.add(role);
//                System.out.println("-------------->"+resultSet.getString("rolename"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
        return roleList;
    }
    
    @Override
    public int insert(Role role) {
        int key = 0; //新增数据的id
        try {
            String sql="insert into role values(null,?,?)";
            List params=new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            int  update = update(sql, params);//update变量保存的是受影响的行数
            
            ResultSet generatedKeys = pps.getGeneratedKeys();
            if(generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
//        System.out.println("roleid--------"+key);
        return key;
    }
    
    @Override
    public Role findById(int roleId) {
        Role role = new Role();
        List menuList=new ArrayList();
        try {
            String sql="select * from role r,menu m,mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
            List params=new ArrayList();
            params.add(roleId);
            
            resultSet=query(sql,params);
            while(resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpmenuId(resultSet.getInt("upmenuid"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int deleteRole(int roleId) {
        int i = 0;
        try {
            String sql = "delete from role where roleid = ?";
            List params = new ArrayList();
            params.add(roleId);
            i = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public int updateRole(Role role) {
        int update = 0;
        try {
            String sql = "update role set rolename = ? and rolestate = ? where roleid = ?";
            List params = new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            params.add(role.getRoleId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int total() {
        int total=0;
        try {
            String sql="select count(1) from role r  ";
            resultSet = query(sql, null);
            while(resultSet.next()){
                total=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }
}
